package com.pz.cloud.framework.society.model;

import com.pz.cloud.framework.base.expection.CustomRuntimeException;
import com.pz.cloud.framework.business.leagueMem.entity.LeagueMem;
import com.pz.cloud.framework.business.leagueMem.service.ILeagueMemService;
import com.pz.cloud.framework.business.society.entity.OrgSociety;
import com.pz.cloud.framework.business.society.service.IOrgSocietyService;
import com.pz.cloud.framework.business.workflow.enums.WorkflowEnum;
import org.apache.commons.lang.StringUtils;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.common.model.BaseImportModel;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * 社团导入
 * @author mzz
 */
@Component
public class OrgSocietyImportModel extends BaseImportModel<OrgSociety> {

    @Autowired
    private ISysDepartService departService;
    @Autowired
    private ISysUserService sysUserService;
    @Autowired
    private IOrgSocietyService orgSocietyService;

    /**
     * 获取导入模板文件
     * @return
     */
    @Override
    protected  String getTemplateFilePath(){
        String path = "excelTemplate/import/orgSociety.xlsx";
        return path;
    }

    @Override
    protected ImportValidetor validateEmptyField() {
        ImportValidetor importValidetor = ImportValidetor.instanec().addValidate("name", "社团名称")
                .addValidate("departName","所属部门名称")
                .addValidate("responsibleManNo", "负责人学号")
                .addValidate("instructorNo", "指导老师工号");
        return importValidetor;
    }

    /**
     * 保存导入的数据
     */
    @Override
    public void saveData(OrgSociety data){
        List<SysDepart> sysDeparts = departService.queryDepartsByDepartName(data.getDepartName());
        if(!CollectionUtils.isEmpty(sysDeparts)){
            String orgCode = sysDeparts.get(0).getOrgCode();
            data.setSysOrgCode(orgCode);
        }
        SysUser sysUser = sysUserService.getUserByName(data.getResponsibleManNo().trim());
        if(sysUser==null){
            throw new CustomRuntimeException("负责人学号【"+data.getResponsibleManNo()+"】不存在！");
        }
        SysUser instructor = sysUserService.getUserByName(data.getInstructorNo().trim());
        if(instructor==null){
            throw new CustomRuntimeException("指导老师工号【"+data.getInstructorNo()+"】不存在！");
        }
        data.setResponsibleMan(sysUser.getRealname());
        data.setResponsibleManId(sysUser.getId());
        data.setInstructorId(instructor.getId());
        data.setInstructor(instructor.getRealname());
        data.setActStatus(WorkflowEnum.ProcessStatus.流程结束.getStatus());
        orgSocietyService.save(data);
    }


    /**
     * 导入模板名称
     * @return
     */
    @Override
    public String getDemoFileName(){
            return "社团导入模板";
    }

}
